﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common.BL;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_in_PlugInInput : System.Web.UI.UserControl
{
    public string _ErrorMessage = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.chkActive.Value = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        if (!IsPostBack)
        {
            string strAction = Request.QueryString["action"];
            string strID = Request.QueryString["id"];

            Plugin plugin;
            NTTHAO.Common.BL.AdminUser loggedUser;
            try
            {
                loggedUser = new NTTHAO.Common.BL.AdminUser(Context.User.Identity.Name);
                if (loggedUser.ID < 1 || !loggedUser.UserName.Equals("admin", StringComparison.OrdinalIgnoreCase))
                {
                    throw new Exception();
                }
                if (strAction.Equals("edit", StringComparison.OrdinalIgnoreCase))
                {
                    int id = Int32.Parse(strID);
                    plugin = new Plugin(id);

                    txtName.Text = plugin.Name;
                    txtAdminFileURL.Text = plugin.AdminManagerURL;
                    txtDescription.Text = plugin.Description;
                    txtInstallFileURL.Text = plugin.InstallerURL;
                    txtPluginID.Value = plugin.ID.ToString();
                    txtUIFileURL.Text = plugin.UIManagerURL;
                    txtUrlName.Text = plugin.PluginURLName;
                    chkActive.Checked = plugin.Actived;
                }
                else if (strAction.Equals("add", StringComparison.OrdinalIgnoreCase))
                {
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Response.Redirect("?type=plug_in&action=list");
                return;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string strAction = Request.QueryString["action"];
            string strID = Request.QueryString["id"];

            Plugin plugin;
            if (strAction != null)
            {
                if (strAction.Equals("edit", StringComparison.OrdinalIgnoreCase))
                {
                    if (!this.EnteredValuesCheck(false))
                    {
                        return;
                    }

                    int id = Int32.Parse(strID);
                    plugin = new Plugin(id);

                    plugin.Name = txtName.Text;
                    plugin.AdminManagerURL = txtAdminFileURL.Text.Trim();
                    plugin.Description = txtDescription.Text.Trim();
                    plugin.InstallerURL = txtInstallFileURL.Text.Trim();
                    plugin.UIManagerURL = txtUIFileURL.Text.Trim();
                    plugin.PluginURLName = txtUrlName.Text.Trim();
                    plugin.Actived = chkActive.Checked;

                    plugin.Update();

                    Response.Redirect("?type=plug_in&action=list");
                }
                else if (strAction.Equals("add", StringComparison.OrdinalIgnoreCase))
                {
                    if (!this.EnteredValuesCheck(true))
                    {
                        return;
                    }

                    plugin = new Plugin();

                    plugin.Name = txtName.Text;
                    plugin.AdminManagerURL = txtAdminFileURL.Text.Trim();
                    plugin.Description = txtDescription.Text.Trim();
                    plugin.InstallerURL = txtInstallFileURL.Text.Trim();
                    plugin.UIManagerURL = txtUIFileURL.Text.Trim();
                    plugin.PluginURLName = txtUrlName.Text.Trim();
                    plugin.Actived = chkActive.Checked;
                    plugin.Installed = false;
                    plugin.LastUpdate = DateTime.Now;
                    plugin.Optional01 = "";
                    plugin.Optional02 = "";
                    plugin.Optional03 = "";

                    plugin.Insert();

                    Response.Redirect("?type=plug_in&action=list");
                }
                else
                {
                    Response.Redirect("?type=plug_in&action=list");
                }
            }
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message;
        }
    }

    private bool EnteredValuesCheck(bool checkExistingName)
    {
        if (checkExistingName)
        {
            string urlName = txtUrlName.Text.Trim();
            try
            {
                Plugin plugin = new Plugin(urlName);
                if (plugin.ID > 0) // existing
                {
                    this._ErrorMessage = "Tên URL này đã có. Vui lòng sử dụng tên khác.";
                    return false;
                }
            }
            catch (Exception ex)
            {
                this._ErrorMessage = ex.Message + "<br /><br />" + ex.StackTrace;
                return false;
            }
        }

        try
        {
            string virtURL = "";
            
            virtURL = "webparts/Plug-ins/" + txtAdminFileURL.Text.Trim();
            if (!System.IO.File.Exists(Server.MapPath(virtURL)))
            {
                this._ErrorMessage = "File quản lý plug-in trang admin không tồn tại.";
                return false;
            }

            virtURL = "webparts/Plug-ins/" + txtUIFileURL.Text.Trim();
            if (!System.IO.File.Exists(Server.MapPath(virtURL)))
            {
                this._ErrorMessage = "File quản lý plug-in trang người dùng không tồn tại.";
                return false;
            }

            virtURL = "webparts/Plug-ins/" + txtInstallFileURL.Text.Trim();
            if (!System.IO.File.Exists(Server.MapPath(virtURL)))
            {
                this._ErrorMessage = "File cài đặt plug-in không tồn tại.";
                return false;
            }
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message + "<br /><br />" + ex.StackTrace;
            return false;
        }

        return true;
    }
}
